﻿using Furion;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Linq;

namespace JiaNetFrame.EntityFramework.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            string connectionString = App.Configuration["DataBase:MySQL:ConnectionString"];
            string dbTypeStr = App.Configuration["DataBase:DbType"].ToLower();
            DbType dbType = DbType.MySql;
            if ("sqlserver".Equals(dbTypeStr))
            {
                dbType = DbType.SqlServer;
                connectionString = App.Configuration["DataBase:SqlServer:ConnectionString"];
            }
            else if("sqlite".Equals(dbTypeStr))
            {
                dbType = DbType.Sqlite;
                connectionString = App.Configuration["DataBase:Sqlite:ConnectionString"];
            }

            services.AddSqlSugar(new ConnectionConfig
            {
                ConnectionString = connectionString,//连接符字串
                DbType = dbType,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
            },
            db =>
            {
                //处理日志事务
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);
                    //Console.WriteLine(string.Join(",", pars?.Select(it => it.ParameterName + ":" + it.Value)));
                    Console.WriteLine();
                };
            });

            //services.AddDatabaseAccessor(options =>
            //{
            //    options.AddDbPool<DefaultDbContext>();
            //}, "JiaNetFrame.Database.Migrations");
        }
    }
}